﻿using System;

using Exif.Exceptions;
using Exif.Interfaces;

using PostSharp.Laos;

namespace Exif.Aspects
{
	///<summary>
	///Атрибут, запрещающий редактирование, если в объекте реализующем <see cref="IReadOnly"/> оно запрещено
	///</summary>
	[Serializable]
	public class ReadOnlyAttribute : OnMethodInvocationAspect
	{
		///<summary>Method called instead of the intercepted method.</summary>
		///<param name="eventArgs">Event arguments specifying which method is being
		///executed and which are its arguments. The implementation should set the return
		///value and output arguments.</param>
		public override void OnInvocation(MethodInvocationEventArgs eventArgs)
		{
			var instance = eventArgs.Delegate.Target as IReadOnly;
			if (instance != null && instance.ReadOnly)
			{
				throw new ReadOnlyAccessException();
			}

			eventArgs.Proceed();
		}
	}
}